<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .item {
            width: 100px;
            height: 100px;
        }

        .red {
            background: red;
        }

        .green {
            background: green;
        }
    </style>
</head>

<body>
    <div class="item red">

    </div>

    <script>
        var div = document.querySelector(".red");
        div.onclick = function() {
            // if (this.className.search(/\bred\b/) !== -1) {
            //     this.className = this.className.replace(/\bred\b/, "green")
            // } else {
            //     this.className = this.className.replace(/\bgreen\b/, "red")
            // }
            if (this.classList.contains("red")) {
                this.classList.remove("red");
                this.classList.add("green");
            } else {
                this.classList.remove("green");
                this.classList.add("red");
            }
        }
    </script>
</body>

</html>